/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Maja Gliese
 */
public class Indstillinger extends javax.swing.JDialog {

    private IniFileReaderWriter iniFileReaderWriter;
    private Main main;
    public Indstillinger(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        main = (Main) parent;
        initComponents();
        iniFileReaderWriter = new IniFileReaderWriter("localhost", 3306, "nvt", "root", "root");
        try {
            iniFileReaderWriter.readFilE();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Inifilen blev ikke fundet");
        } catch (IOException ex) {
            Logger.getLogger(Indstillinger.class.getName()).log(Level.SEVERE, null, ex); 
        }
        setFields();
    }
    
    private void setFields(){
        HostTxt.setText(iniFileReaderWriter.getHost());
        PortNrTxt.setText(iniFileReaderWriter.getPortnr() + "");
        DatabaseTxt.setText(iniFileReaderWriter.getDatabase());
        UsernameTxt.setText(iniFileReaderWriter.getUdb());
        passwordTxt.setText(iniFileReaderWriter.getPdb());
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        HostLabel = new javax.swing.JLabel();
        PortNrLabel = new javax.swing.JLabel();
        DatabaseLabel = new javax.swing.JLabel();
        UsernameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        HostTxt = new javax.swing.JTextField();
        PortNrTxt = new javax.swing.JTextField();
        DatabaseTxt = new javax.swing.JTextField();
        UsernameTxt = new javax.swing.JTextField();
        passwordTxt = new javax.swing.JTextField();
        OverskriftLabel = new javax.swing.JLabel();
        ForbindKnap = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jLabel3.setText("jLabel3");

        jTextField6.setText("jTextField6");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Indstillinger");
        setAlwaysOnTop(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setOpaque(false);

        HostLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        HostLabel.setForeground(new java.awt.Color(255, 255, 255));
        HostLabel.setText("Host/IP:");

        PortNrLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        PortNrLabel.setForeground(new java.awt.Color(255, 255, 255));
        PortNrLabel.setText("Portnr:");

        DatabaseLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        DatabaseLabel.setForeground(new java.awt.Color(255, 255, 255));
        DatabaseLabel.setText("Database:");

        UsernameLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        UsernameLabel.setForeground(new java.awt.Color(255, 255, 255));
        UsernameLabel.setText("UserName:");

        PasswordLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        PasswordLabel.setForeground(new java.awt.Color(255, 255, 255));
        PasswordLabel.setText("Password:");

        OverskriftLabel.setFont(new java.awt.Font("Garamond", 0, 36)); // NOI18N
        OverskriftLabel.setForeground(new java.awt.Color(255, 255, 255));
        OverskriftLabel.setText("Database login");

        ForbindKnap.setText("Forbind");
        ForbindKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ForbindKnapActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(OverskriftLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PortNrLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HostLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DatabaseLabel)
                            .addComponent(UsernameLabel)
                            .addComponent(PasswordLabel))
                        .addGap(69, 69, 69)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(ForbindKnap))
                            .addComponent(UsernameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DatabaseTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HostTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(PortNrTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(OverskriftLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HostLabel)
                    .addComponent(HostTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PortNrLabel)
                    .addComponent(PortNrTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DatabaseLabel)
                    .addComponent(DatabaseTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UsernameLabel)
                    .addComponent(UsernameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PasswordLabel)
                            .addComponent(passwordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ForbindKnap)
                        .addGap(21, 21, 21))))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 420, 320));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/Baggrund 2.PNG"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-7, -7, 430, 340));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ForbindKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ForbindKnapActionPerformed
        iniFileReaderWriter.setHost(HostTxt.getText());
        iniFileReaderWriter.setPortnr(Integer.parseInt(PortNrTxt.getText()));
        iniFileReaderWriter.setDatabase(DatabaseTxt.getText());
        iniFileReaderWriter.setUdb(UsernameTxt.getText());
        iniFileReaderWriter.setPdb(passwordTxt.getText());
       
        try {
            iniFileReaderWriter.writeFile();
        } catch (IOException ex) {
           JOptionPane.showMessageDialog(this, "Kan ikke skrive til inifilen");          
        }
        
        main.instantierSQLIgen();
        
        
        try {
            main.getSql().openConn();
            JOptionPane.showMessageDialog(this, "Der er oprettet forbindelse til databasen");
            setVisible(false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Kunne ikke etablere forbindelse til databasen");
        }
        
        
            
        
        
    }//GEN-LAST:event_ForbindKnapActionPerformed
    
    
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Indstillinger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Indstillinger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Indstillinger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Indstillinger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Indstillinger dialog = new Indstillinger(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DatabaseLabel;
    private javax.swing.JTextField DatabaseTxt;
    private javax.swing.JButton ForbindKnap;
    private javax.swing.JLabel HostLabel;
    private javax.swing.JTextField HostTxt;
    private javax.swing.JLabel OverskriftLabel;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JLabel PortNrLabel;
    private javax.swing.JTextField PortNrTxt;
    private javax.swing.JLabel UsernameLabel;
    private javax.swing.JTextField UsernameTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField passwordTxt;
    // End of variables declaration//GEN-END:variables
}
